package dbexp.test;

import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import dbexp.connection.DbAccessException;
import dbexp.connection.DbConnection;
import dbexp.connection.PoolManagerException;
import dbexp.connection.DbMetaData;
import dbexp.connection.DbStateException;
import dbexp.model.Column;
import dbexp.model.TableData;
import dbexp.model.db.DbUser;

public class JSONtest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DbUser u = new DbUser("ORA9I", "localhost", "qazxsw", 1521, "SYSTEM");
		try {
			u = DbMetaData.createConnection(u);
			TableData td = new TableData("TEST", "MICHAL");// DbMetaData.getData(u,
															// "SELECT USERNAME FROM ALL_USERS",2,10);

			String dane1 = "{\"name\":\"xx\",\"dataType\":\"VARCHAR2\",\"size\":\"10\",\"scale\":\"0\",\"nullable\":false,\"default\":\"\"}";

			@SuppressWarnings("unused")
			String dane2 = "[{\"name\":\"xx\",\"dataType\":\"VARCHAR2\",\"size\":\"10\",\"scale\":\"0\",\"nullable\":false,\"default\":\"\"},{\"name\":\"xx\",\"dataType\":\"VARCHAR2\",\"size\":\"10\",\"scale\":\"0\",\"nullable\":false,\"default\":\"\"}]";

			dane1 = "[XXX,AAA]";

			JSONArray data = null;
			try {
				data = new JSONArray(dane1);
			} catch (JSONException e) {
				try {
					data = new JSONArray("[" + dane1 + "]");
				} catch (JSONException e1) {
					e1.printStackTrace();
				}
			}
			for (int i = 0; i < data.length(); i++) {
				try {
					JSONObject obj = null;
					if (data.get(i) instanceof JSONObject) {
						obj = data.getJSONObject(i);
					} else {
						obj = new JSONObject().put("name", data.get(i));
					}
					Column col = new Column(obj.getString("name"), "MICHAL",
							"TEST");
					// col.setDataType(obj.getString("dataType"));
					// col.setDataSize(obj.getLong("size"));
					// col.setDataScale(obj.getString("scale"));
					// col.setNullable(obj.getBoolean("nullable"));
					// col.setDataDefault(obj.getString("default"));
					td.addColumn(col);

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
			System.out.println(td.getQuery(DbConnection.CREATE));
			// List<Column> columny = td.getColumns();
			// List<List<Object>> rows = td.getRows();
			// for (int i = 0; i < columny.size(); i++) {
			// System.out.print(columny.get(i)+"\t");
			// }
			// System.out.println();
			// int c=0;
			// for (List<Object> list : rows) {
			// for (Object object : list) {
			// System.out.print(object+"\t");
			// }
			// System.out.println();
			// c++;
			// }
			System.out.println(data);

		} catch (PoolManagerException e) {
			e.printStackTrace();
		} catch (DbAccessException e) {
			e.printStackTrace();
		} catch (DbStateException e) {
			e.printStackTrace();
		}
		// catch (DbQueryException e) {
		// e.printStackTrace();
		// }
		catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
